package targetToOffer.eighteenthDay;

import java.util.Arrays;

/**
 * @author Godc
 * @description
 */
public class ConstructArr {
    public static void main(String[] args) {
        ConstructArrSolution s = new ConstructArrSolution();
        int[] ints = s.constructArr(new int[]{1,2});
        System.out.println(Arrays.toString(ints));
    }
}
class ConstructArrSolution {
    public int[] constructArr(int[] a) {
        // 求前缀和
        int[] preSum = new int[a.length];
        int sum = 1;
        for(int i = 0;i<a.length;i++){
            sum*=a[i];
            preSum[i] = sum;
        }
        // 求后缀和
        int[] postSum = new int[a.length];
        sum = 1;
        for(int i = a.length-1;i>=0;i--){
            sum*=a[i];
            postSum[i] = sum;
        }
        int[] res = new int[a.length];
        for(int i = 0;i<a.length;i++){
            if(i-1>=0&&i+1<a.length) res[i] = preSum[i-1]*postSum[i+1];
            if(i-1<0) res[i] = postSum[i+1];
            if(i+1>=a.length) res[i] = preSum[i-1];
        }
        return res;
    }
}